package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
//查询预约天数
    @Override
    public List<Map<String, Object>> getOrderSettingByDate(String date) {
        List<OrderSetting> allByDate = orderSettingDao.getAllByDate(date);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (OrderSetting orderSetting : allByDate) {
            Date orderDate = orderSetting.getOrderDate();
            String s = orderDate.toString();
            String[] s1 = s.split(" ");
            String dates = s1[2];
            Map<String, Object> map = new HashMap<>();
            map.put("date", dates);
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            maps.add(map);
        }
        return maps;
    }

    //修改预约天数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) throws ParseException {
        Date orderDates = orderSetting.getOrderDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(orderDates);
        Date orderDate = sdf.parse(s);
        int number = orderSetting.getNumber();
        OrderSetting orderSettings = orderSettingDao.selectByDate(orderDate);
        if (orderSettings != null) {
            orderSettingDao.editNumberByDate(number, orderDate);
        } else {
            orderSettingDao.insertOrderSetting(number, orderDate);
        }
    }
//Excel表格导入
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            Date orderDate = orderSetting.getOrderDate();
            OrderSetting orderSetting1 = orderSettingDao.selectByDate(orderDate);
            if (orderSetting1 != null) {
                orderSettingDao.editNumberByDate(orderSetting.getNumber(), orderDate);
            } else {
                orderSettingDao.insertOrderSetting(orderSetting.getNumber(), orderDate);
            }
        }
    }

}
